//
//  BPScreenFileManager.m
//  BPScreenManager_Example
//
//  Created by SiYugui on 2020/4/30.
//  Copyright © 2020年 xuhongji. All rights reserved.
//

#import "BPScreenFileManager.h"

static NSString* const kPath = @"BPScrentManager/myScreenShot";

@implementation BPScreenFileManager

#pragma mark - 文件地址

+ (NSString*)filePath{
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString *desFileName = [documentPath stringByAppendingPathComponent:kPath];
    return desFileName;
}

+ (NSString*)fileName{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS";
    NSString *fileName = [NSString stringWithFormat:@"screenshot_iOS_%@.jpeg",[formatter stringFromDate:[NSDate date]]];
    return fileName;
}

+ (NSArray<NSString *> *)allFilesWithPath:(NSString *)path{
    return [self filesWithPath:path];
}

+ (NSArray *)allFilePathArrayWithSplitSize:(int)size{
    
    NSArray *array = [self allFilesWithPath:[self filePath]];
    return [self splitArray:array withSubSize:size];
}

+ (NSData *)dataInJpegFormatWithImage:(UIImage*)image compressionQuality:(float)compression{
    return UIImageJPEGRepresentation(image, compression);
}

#pragma mark - 文件存储(增删改查)

+ (BOOL)saveFile:(NSData*)fileData fileName:(NSString*)fileName ToPath:(NSString*)path{
    
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return [fileData writeToFile:filePath atomically:YES];
}

+ (BOOL)deleteFile:(NSString*)fileName atPath:(NSString*)path{
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    return [self deleteFileWithPath:filePath];
}

+ (BOOL)deleteFileWithPath:(NSString*)filePath{
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]&&[[NSFileManager defaultManager] isDeletableFileAtPath:filePath]) {
        NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        if (!error) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - private
/**
 拆分数值
 
 @param array 源数据
 @param subSize 拆分size
 @return 重组的数据
 */
+ (NSArray *)splitArray: (NSArray *)array withSubSize : (int)subSize{
    
    //  数组将被拆分成指定长度数组的个数
    unsigned long count = array.count % subSize == 0 ? (array.count / subSize) : (array.count / subSize + 1);
    //  用来保存指定长度数组的可变数组对象
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    
    //利用总个数进行循环，将指定长度的元素加入数组
    for (int i = 0; i < count; i ++) {
        //数组下标
        int index = i * subSize;
        //保存拆分的固定长度的数组元素的可变数组
        NSMutableArray *arr1 = [[NSMutableArray alloc] init];
        //移除子数组的所有元素
        [arr1 removeAllObjects];
        
        int j = index;
        //将数组下标乘以1、2、3，得到拆分时数组的最大下标值，但最大不能超过数组的总大小
        while (j < subSize*(i + 1) && j < array.count) {
            [arr1 addObject:[array objectAtIndex:j]];
            j += 1;
        }
        //将子数组添加到保存子数组的数组中
        [arr addObject:[arr1 copy]];
    }
    
    return [arr copy];
}


+ (NSArray *)filesWithPath:(NSString*)path{//获取路径下所有文件地址
    NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:path];
    NSString *fileName;
    NSMutableArray *arrayM = [NSMutableArray array];
    while (fileName = [dirEnum nextObject]) {
        NSString *filePath = [path stringByAppendingPathComponent:fileName];
        [arrayM addObject:filePath];
    }
    return arrayM;
}

@end
